﻿using Qy.Common;
using Qy.Models;
using PetaPoco;
using PetaPoco.Core;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Qy.Repository
{
    [AppService]
    public class UserRepository : BaseRepository<User, PetaPocoContext>
    {

        public UserRepository(PetaPocoContext petaPocoContext) : base(petaPocoContext) { }
        /// <summary>
        /// 联表查询角色
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task<ViewUser> GetUserInfoAsync(int userId)
        {
            Sql sql = Sql.Builder;
            sql.Select("dept.DeptName,dept.Id as DeptId,post.PostName,post.PostCode,post.Id as PostId,B.Groupname,user.*")
                .From("qy_admin_user as user")
                .LeftJoin("qy_admin_group as B").On("user.Gid = B.Id");
            sql.LeftJoin("qy_admin_dept as dept").On("user.DeptId = dept.Id");
            sql.LeftJoin("qy_admin_post as post").On("user.PostId = post.Id");
            sql.Where("user.Id=@0", userId);
            return await CreateDAO().FirstOrDefaultAsync<ViewUser>(sql);
        }
        /// <summary>
        /// 联表查询角色
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task<Page<ViewUser>> GetPageListAsync(UserQuery query, int gid = 0)
        {
            Sql sql = Sql.Builder;
            sql.Select("dept.DeptName,post.PostName,post.PostCode,B.Groupname,A.*")
                .From("qy_admin_user as A")
                .LeftJoin("qy_admin_group as B").On("A.Gid = B.Id");
            sql.LeftJoin("qy_admin_dept as dept").On("A.DeptId = dept.Id");
            sql.LeftJoin("qy_admin_post as post").On("A.PostId = post.Id");
            if (gid != 1)
            {
                sql.Where("A.Gid != 1");
            }
            if (query.DeptId != 0)
                sql.Where("A.DeptId = @0", query.DeptId);

            if (!string.IsNullOrEmpty(query.NickName))
                sql.Where("A.Nickname like @0", "%" + query.NickName + "%");

            if (!string.IsNullOrEmpty(query.UserName))
                sql.Where("A.Username like @0", "%" + query.UserName + "%");

            if (query.Status != 2)
                sql.Where("A.Status = @0", query.Status);

            sql.OrderBy("A.Id desc");
            // Console.WriteLine(sql.ToString());
            return await CreateDAO().PageAsync<ViewUser>(query.Page, query.Limit, sql);
        }

        /// <summary>
        /// 查询指定列  ID  Name 
        /// </summary>
        /// <returns></returns>
        public async Task<List<UserIdAndName>> GetNameAsync(int userId, string name = null)
        {
            Sql sql = Sql.Builder;
            sql.Select("A.Id,A.Username,Nickname,A.Status,dept.DeptName")
                .From("qy_admin_user as A")
                .LeftJoin("qy_admin_dept as dept").On("A.DeptId = dept.Id");
            if (!string.IsNullOrEmpty(name))
                sql.Where("A.Nickname like @0", "%" + name + "%");

            if (userId > 0)
                sql.Where("A.Id !=@0 ", userId);

            sql.Where("A.Status = 1");
            sql.OrderBy("dept.OrderNum desc");
            return await CreateDAO().FetchAsync<UserIdAndName>(sql);
        }



        /// <summary>
        /// 判断当前用户名是否存在
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public async Task<bool> GetByUsernameAsync(string username)
        {
            PocoData data = PocoData.ForType(typeof(User), new ConventionMapper());
            Sql sql = Sql.Builder;
            sql.Select("*");
            sql.From(new object[] { data.TableInfo.TableName });
            sql.Where("Username =@0", username);

            return await CreateDAO().FirstOrDefaultAsync<User>(sql) == null;
        }

        public async Task<bool> GetByGidAsync(int gid)
        {
            PocoData data = PocoData.ForType(typeof(User), new ConventionMapper());
            Sql sql = Sql.Builder;
            sql.Select("*");
            sql.From(new object[] { data.TableInfo.TableName });
            sql.Where("Gid =@0", gid);

            return await CreateDAO().FirstOrDefaultAsync<User>(sql) != null;
        }



        /// <summary>
        /// 判断当前部门下有没有用户
        /// </summary>
        /// <param name="deptId"></param>
        /// <returns></returns>

        public async Task<bool> GetUserByDeptIdAsync(int deptid)
        {
            PocoData data = PocoData.ForType(typeof(User), new ConventionMapper());
            Sql sql = Sql.Builder;
            sql.Select("*");
            sql.From(new object[] { data.TableInfo.TableName });
            sql.Where("DeptId =@0", deptid);

            return await CreateDAO().FirstOrDefaultAsync<User>(sql) != null;
        }
        public async Task<User> CheckUorP(string username, string md5Password)
        {
            PocoData data = PocoData.ForType(typeof(User), new ConventionMapper());
            Sql sql = Sql.Builder;
            sql.Select("*");
            sql.From(new object[] { data.TableInfo.TableName });
            sql.Where("Username =@0 AND Password = @1", username, md5Password);

            return await CreateDAO().FirstOrDefaultAsync<User>(sql);
        }

    }
}
